package org.boticelli.controller;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.boticelli.Bot;
import org.boticelli.dao.LogDAO;
import org.boticelli.model.LogEntry;
import org.boticelli.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller handling the display of logs.
 *
 * @author shelmberger
 */
@Controller
public class LogController extends AbstractBotController
{
    protected static Logger log = Logger.getLogger(LogController.class);

    private LogDAO logDAO;

    @Autowired(required = true)
    public void setLogDAO(LogDAO logDAO)
    {
        this.logDAO = logDAO;
    }

    /**
     * Shows the log of a given day.
     *
     * @param response
     * @param botId
     *            Id of the bot as defined in the bot context. Can be
     *            <code>null</code> if there is only one bot defined.
     * @param day
     *            day of month if <code>null</code> the current day is used.
     * @param month
     *            month if <code>null</code> the current day is used.
     * @param year
     *            year if <code>null</code> the current day is used.
     * @return
     */
    @RequestMapping("/log")
    public ModelAndView showLog(HttpServletResponse request, HttpServletResponse response,
            @RequestParam(value = "bot", required = false) String botId,
            @RequestParam(value = "day", required = false) String day, String error, String search)
    {
        Bot bot = findBot(botId);

        Calendar today = DateUtil.getCalendarFor(day);

        List<LogEntry> logs = logDAO.findByDay(DateUtil.daySinceEpoch(today));

        if (log.isDebugEnabled())
        {
            log.debug("Showing logs from " + today.getTime());
        }

        Calendar tomorrow = (Calendar) today.clone();
        tomorrow.add(Calendar.DATE, 1);

        Calendar yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, -1);

        ModelAndView mav = new ModelAndView("log");
        mav.addObject("bot", bot);
        mav.addObject("logs", DecoratedLog.decorate(logs));
        mav.addObject("today", today.getTime());
        mav.addObject("prevURI", logURI(bot, yesterday));
        mav.addObject("nextURI", logURI(bot, tomorrow));
        mav.addObject("isSearch", false);
        mav.addObject("searchError", error);
        mav.addObject("search", search);
        return mav;
    }

    /**
     * Returns the context relative URL of that calls the log controller on the
     * given calendar date.
     *
     * @param cal
     * @return
     */
    private String logURI(Bot bot, Calendar cal)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("/app/log?bot=").append(bot.getBeanName()).append("&day=").append(DateUtil.toYYYYMMDD(cal));

        return sb.toString();
    }

}
